################################################################################
#
#Copyright (c) 2011, Petr Skramovsky
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL Petr Skramovsky BE LIABLE FOR ANY
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################

import c4d
from c4d import bitmaps
from c4d import gui
from c4d import plugins
from c4d import storage
import os
from qmapex import *

PLUGIN_ID = 1027446

class ExportDlg(gui.GeDialog):
  def __init__(self):
    self.ID_TXT_PATH = 1001
    self.ID_TXT_SCALE = 1002
    self.ID_TXT_GRID = 1003
    self.ID_TXT_EXT = 1004
    self.ID_TXT_TEX = 1005
    self.ID_EDIT_PATH = 2001
    self.ID_EDIT_TEX = 2002
    self.ID_BTN_SAVE = 3001
    self.ID_BTN_EXPORT = 3002
    self.ID_GROUP_MAIN = 4001
    self.ID_GROUP_FILE = 4002
    self.ID_GROUP_COPYRIGHT = 4003
    self.ID_SLD_SCALE = 5001
    self.ID_SLD_GRID = 5002
    self.ID_SLD_EXT = 5003
    self.ID_CHCK_POLY = 6001
    self.ID_CHCK_LIGHT = 6002
    self.ID_CHCK_NURBS = 6003
    self.ID_CHCK_COORD = 6004
    self.ID_CHCK_NORMAL = 6005
    self.ID_CHCK_CAULK = 6006
    self.COPYRIGHT = '\ Copyright 2011, Petr "CPU-killer" Skramovsky'
    self.VERSION = 0.2
    self.DEFAULT_TEXTURE = "radiant/notex"

  def SetDefaultValues(self):
    self.SetReal(self.ID_SLD_SCALE, value=1.0, min=0.1, max=10, step=0.1)
    self.SetReal(self.ID_SLD_GRID, value=0, min=0, max=128, step=8)
    self.SetReal(self.ID_SLD_EXT, value=32, min=8, max=128, step=8)
    self.SetString(self.ID_EDIT_TEX, self.DEFAULT_TEXTURE)
    self.SetBool(self.ID_CHCK_POLY, True)
    self.SetBool(self.ID_CHCK_LIGHT, True)
    self.SetBool(self.ID_CHCK_NURBS, True)
    self.SetBool(self.ID_CHCK_COORD, True)
    self.SetBool(self.ID_CHCK_NORMAL, True)
    self.SetBool(self.ID_CHCK_CAULK, True)

  def InitValues(self):
    self.SetDefaultValues()
    return True      

  def CreateLayout(self):
    self.SetTitle("Quake .MAP exporter %0.2f" % self.VERSION)
    self.GroupBegin(self.ID_GROUP_MAIN, c4d.BFH_SCALEFIT, cols=2, rows=5)
    self.AddStaticText(self.ID_TXT_PATH, c4d.BFH_LEFT, name="Output file : ")
    self.GroupBegin(self.ID_GROUP_MAIN, c4d.BFH_SCALEFIT, cols=2, rows=1)
    self.AddEditText(self.ID_EDIT_PATH, c4d.BFH_SCALEFIT)
    self.AddButton(self.ID_BTN_SAVE, c4d.BFH_RIGHT, name="...")
    self.GroupEnd()
    self.AddStaticText(self.ID_TXT_SCALE, c4d.BFH_LEFT, name="Scale : ")
    self.AddEditSlider(self.ID_SLD_SCALE, c4d.BFH_SCALEFIT)
    self.AddStaticText(self.ID_TXT_GRID, c4d.BFH_LEFT, name="Grid snap : ")
    self.AddEditSlider(self.ID_SLD_GRID, c4d.BFH_SCALEFIT)
    self.AddStaticText(self.ID_TXT_EXT, c4d.BFH_LEFT, name="Extrude : ")
    self.AddEditSlider(self.ID_SLD_EXT, c4d.BFH_SCALEFIT)
    self.AddStaticText(self.ID_TXT_TEX, c4d.BFH_LEFT, name="Texture : ")
    self.AddEditText(self.ID_EDIT_TEX, c4d.BFH_SCALEFIT)
    self.GroupEnd()
    self.AddSeparatorH(0)

    self.GroupBegin(self.ID_GROUP_MAIN, c4d.BFH_SCALEFIT, cols=1, rows=3)
    self.AddCheckbox(self.ID_CHCK_POLY, c4d.BFH_SCALEFIT, 0, 0, name="Polygon objects")
    self.AddCheckbox(self.ID_CHCK_LIGHT, c4d.BFH_SCALEFIT, 0, 0, name="Lights")
    self.AddCheckbox(self.ID_CHCK_NURBS, c4d.BFH_SCALEFIT, 0, 0, name="Bezier NURBS")
    self.GroupEnd()
    self.AddSeparatorH(0)

    self.GroupBegin(self.ID_GROUP_MAIN, c4d.BFH_SCALEFIT, cols=1, rows=3)
    self.AddCheckbox(self.ID_CHCK_COORD, c4d.BFH_SCALEFIT, 0, 0, name="Quake coordinate system")
    self.AddCheckbox(self.ID_CHCK_NORMAL, c4d.BFH_SCALEFIT, 0, 0, name="Per-vertex normals")
    self.AddCheckbox(self.ID_CHCK_CAULK, c4d.BFH_SCALEFIT, 0, 0, name="Caulk invisible faces")
    self.GroupEnd()
    self.AddSeparatorH(0)

    self.exp_button = self.AddButton(self.ID_BTN_EXPORT, c4d.BFV_CENTER, name="EXPORT")
        
    #self.GroupBegin( self.ID_GROUP_COPYRIGHT, c4d.BFV_BOTTOM | c4d.BFH_SCALEFIT, title=self.COPYRIGHT )
    #self.GroupBorder( c4d.BORDER_GROUP_TOP )
    #self.GroupEnd()

    return True
    
  def Command(self, id, msg):
    if id == self.ID_BTN_SAVE:
      path = storage.SaveDialog()
      self.SetFilename(self.ID_EDIT_PATH, path)
    elif id == self.ID_BTN_EXPORT:
      path = self.GetFilename(self.ID_EDIT_PATH)
      if path is None or path == "":
        gui.MessageDialog("Select output file first.")
        return
      c4d.StatusSetText("Exporting to .map file...")
      c4d.StatusSetSpin()
      params = self.Params()
      self.Enable(self.exp_button, False)
      try:
        export(self.doc, path, params)
      except Exception, e:
        print e
        gui.MessageDialog( "Some error occured, check python console for more details." )
      c4d.StatusClear()
      self.Enable(self.exp_button, True)

    return True

  def Params(self):
    params = {}
    params[EXP_PARAM_POLY] = self.GetBool(self.ID_CHCK_POLY)
    params[EXP_PARAM_LIGHT] = self.GetBool(self.ID_CHCK_LIGHT)
    params[EXP_PARAM_NURBS] = self.GetBool(self.ID_CHCK_NURBS)
    params[EXP_PARAM_COORD] = self.GetBool(self.ID_CHCK_COORD)
    params[EXP_PARAM_NORMAL] = self.GetBool(self.ID_CHCK_NORMAL)
    params[EXP_PARAM_CAULK] = self.GetBool(self.ID_CHCK_CAULK)
    params[EXP_PARAM_SCALE] = self.GetReal(self.ID_SLD_SCALE)
    params[EXP_PARAM_EXT] = self.GetReal(self.ID_SLD_EXT)
    params[EXP_PARAM_GRID] = self.GetReal(self.ID_SLD_GRID)
    params[EXP_PARAM_TEX] = self.GetString(self.ID_EDIT_TEX)
    return params

  def Show(self):
    self.Open(dlgtype=c4d.DLG_TYPE_MODAL, defaultw=300, defaulth=280, pluginid=PLUGIN_ID)
	
  def SetDoc(self, doc):
    self.doc = doc
		
class Menu(plugins.CommandData):
  dialog = None
  def Execute(self, doc):
    if self.dialog is None:
      self.dialog = ExportDlg()
    self.dialog.SetDoc(doc)
    return self.dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC, defaultw=300, defaulth=280, pluginid=PLUGIN_ID)  

  def RestoreLayout(self, sec_ref):
    if self.dialog is None:
      self.dialog = ExportDlg()
    return self.dialog.Restore(pluginid=PLUGIN_ID, secret=sec_ref)
		
if __name__ == "__main__":
  bmp = bitmaps.BaseBitmap()
  dir, f = os.path.split(__file__)
  fn = os.path.join(dir, "res", "icon.png")
  bmp.InitWith(fn)
  plugins.RegisterCommandPlugin(id=PLUGIN_ID, str="Quake .MAP exporter", help="Quake .MAP exporter",info=0,dat=Menu(), icon=bmp )
